package com.ovt.doorbell.payment.dao;

import java.text.MessageFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.mapper.ResourceMapper;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.Resource;
import com.ovt.doorbell.payment.dao.vo.User;

@Repository
public class ResourceDaoImpl implements ResourceDao
{

    @Autowired
    private DaoHelper daoHelper;
    @Autowired
    private ResourceMapper resourceMapper;
    
    private static final String SQL_INSERT_RESOURCE = "INSERT INTO "
            + "resource(res_code,res_name,res_type,res_price,res_unit,res_size,create_time,update_time,update_by) "
            + "VALUES(?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)";
    
    private static final String SQL_DELETE_RESOURCE = "DELETE FROM resource WHERE id = ?";
    
    private static final String SQL_GET_PREFIX = "SELECT res_code,res_name,res_type,"
            + "res_price,res_unit,res_size,create_time,update_time,update_by "
            + "FROM resource ";
    
    private static final String SQL_GET_RESOURCE_LIST = SQL_GET_PREFIX
            + "order by {0} {1} LIMIT {2}, {3}";
    
    private static final String SQL_UPDATE_RESOURCE = "UPDATE resource SET "
            + "res_code = ?, res_name = ?, res_type = ?, res_price = ?, "
            + "res_unit = ?, res_size = ?, update_time = CURRENT_TIMESTAMP, update_by = ? "
            + "WHERE id = ?";
    
    private static final String SQL_GET_PRICE = "SELECT res_price FROM resource WHERE id = ?";
    
    private static final String SQL_GET_RESOURCE = SQL_GET_PREFIX + "WHERE res_type = ? AND res_size = ?";
    
    private static final String SQL_GET_DEFAULT_RESOURCE = SQL_GET_PREFIX + "WHERE res_price = 0";
    
    @Override
    public void save(Resource res)
    {
        Object[] params = new Object[7];
        params[0] = res.getCode();
        params[1] = res.getName();
        params[2] = res.getType();
        params[3] = res.getPrice();
        params[4] = res.getUnit();
        params[5] = res.getSize();
        params[7] = res.getUpdateBy();
        
        String errMsg = MessageFormat.format("Failed insert resource, code={0}!",
                        res.getCode());

        daoHelper.save(SQL_INSERT_RESOURCE, errMsg, true, params);
        
    }

    @Override
    public void delete(int resId)
    {
        String errMsg =
                MessageFormat.format("Failed to delete resource [{0}]!", resId);
        daoHelper.update(SQL_DELETE_RESOURCE, errMsg, resId);
    }

    @Override
    public void update(Resource res)
    {
        Object[] params = new Object[8];
        params[0] = res.getCode();
        params[1] = res.getName();
        params[2] = res.getType();
        params[3] = res.getPrice();
        params[4] = res.getUnit();
        params[5] = res.getSize();
        params[6] = res.getUpdateTime();
        params[7] = res.getUpdateBy();
        
        String errMsg = MessageFormat.format("Failed update resource, code={0}!",
                        res.getCode());

        daoHelper.update(SQL_UPDATE_RESOURCE, errMsg, params);
    }

    @Override
    public List<Resource> getResourceList(PageInfo page)
    {
        String sql =
                MessageFormat.format(
                        SQL_GET_RESOURCE_LIST,
                        page.getSortBy(),
                        page.getOrder().toString(),
                        DataConvertUtils.toString(page.getPageNo() * page.getPageSize()),
                        DataConvertUtils.toString(page.getPageSize()));

        String errMsg =
                MessageFormat.format(
                        "Failed to query resource list with page info: {0}!",
                        page);

        List<Resource> resources = daoHelper.queryForList(sql, resourceMapper, errMsg);

        return resources;
        
    }
    
    @Override
    public float getPrice(int resId)
    {
        String errMsg =
                MessageFormat.format(
                        "Failed to get resource price with resource ID: {0}!",
                        resId);
        return daoHelper.queryForObject(SQL_GET_PRICE, Float.class, errMsg, resId);
        
    }

    @Override
    public Resource getResource(String type, int size)
    {
        String errMsg =
                MessageFormat.format(
                        "Failed to get resource with resource type: {0} and price: {1}!",
                        type, size);
        
        return daoHelper.queryForObject(SQL_GET_RESOURCE, resourceMapper, errMsg, type, size);
    }
    
    @Override
    public List<Resource> getDefaultResource()
    {
        String errMsg = MessageFormat.format("Failed to get default resource!",(Object[])null);

        List<Resource> resources = daoHelper.queryForList(SQL_GET_DEFAULT_RESOURCE, resourceMapper, errMsg);

        return resources;
    }

}
